/**
 * CLASS
 * 		IconTray
 * DESCRIPTION
 * 		Display icon in trary & handle minimize windows
 * USAGE
 * 		
 * @class
 * @public
 *
 *  Copy from snitter (twitter) + Fresh AIR (sample RSS reader) & Modify to static class by Tae
 * 
 */
IconTray = function() {
	throw(new Error(
			"You cannot instantiate the 'IconTray' class. " +
			"Instead, use 'IconTray.getDefault()' to retrieve the " +
			"class' unique instance."
	));
} 
 
IconTray.getDefault = function(){

	var context = arguments.callee;
	if (context.instance) { return context.instance };

	function _iconTray() {
		//Global Variables.
		var o			 = {};
		var init_attr	 = ['width', 'height','x','y','name','version'];
		
		//Public function --> Copy from Fresh AIR
		this.init = function() {
			var loader = new air.Loader();

			//Load icons events..
			loader.contentLoaderInfo.addEventListener(air.Event.COMPLETE, iconLoadComplete);

			//Resources?
			var request;
			try {
				if(air.NativeApplication.supportsDockIcon){
					request = new air.URLRequest("icons/icon128.png");
				}
				else {
					request = new air.URLRequest("icons/icon16.png");
				}
			} catch(e) {
				air.trace('init on IconTray -> ' + e);
			}
			
			//Variables..			
			var xmlobject    = (new DOMParser()).parseFromString(air.NativeApplication.nativeApplication.applicationDescriptor, "text/xml");
						
			//Looping..
			for(var i = 0; i < init_attr.length;i++) {
				try
				{
					var node   		= xmlobject.getElementsByTagName(init_attr[i])[0];
					var data   		= node.childNodes[0].data;
					o[init_attr[i]]	= data;
				}
				catch(e) {
					air.trace('resize Windows (' + init_attr[i] + ') error : ' + e);
					o[init_attr[i]]	= 0;
				}			
			}
			
			//load icon..
			loader.load(request);    
		}
	
		function getAppName() {
			return o[init_attr[4]] + ' ' + o[init_attr[5]];
		}
		
		this.getDesc	= function() {
			return getAppName();
		}
	
		this.minimizeToTray =  function(e) {
			
			try {
				if(e.afterDisplayState=='minimized'){
					e.preventDefault();
					nativeWindow.minimize();
					nativeWindow.visible = false;
				} else {
					air.trace(' --> ' + e.afterDisplayState);
				}
			} catch(e) {
				air.trace('Error on Minimize to tray -> ' + e);
			}
		}
	
		this.close			= function() {
		/***  TAE 2008.09.06
			var closing = new air.Event(air.Event.CLOSING, true, true);
			window.nativeWindow.dispatchEvent(closing);
			if(!closing.isDefaultPrevented()){
				window.close();
			}
			**/
			air.NativeApplication.nativeApplication.icon.bitmaps = [];
            air.NativeApplication.nativeApplication.exit();
		}
	
		this.makeWindowVisible = function(e) {
			showApplication(e);
		}
	
		function showApplication(event) {
			if(nativeWindow.displayState == air.NativeWindowDisplayState.MINIMIZED){
				nativeWindow.restore();
			}			
			nativeWindow.visible = true;
			nativeWindow.activate();
			
			nativeWindow.orderToFront();		//Thanks to snitter.
		}
	
		function hide(evt) {
			nativeWindow.visible = false
		}
	
		this.addNotification = function(title, message) {
			//Implement later...
			air.trace('add Notification...');
		}
		
		this.resetSize 		 = function() {
			 reset_size();
		}
		
		function onExit(evt) {
			//Terminate...
			try {		
				air.NativeApplication.nativeApplication.icon.bitmaps = [];
				air.NativeApplication.nativeApplication.exit();
			} catch(ex) {
				dump('Tray closed : ' + ex);
			}
		}
		
		function reset_size() {
			//Set
			nativeWindow.x 		= o[init_attr[2]];
			nativeWindow.y 		= o[init_attr[3]];
			nativeWindow.width 	= o[init_attr[0]];
			nativeWindow.height = o[init_attr[1]];
		}
					
		function iconLoadComplete(event) {
			
			var bitmap = event.target.loader.content;
		    
			air.NativeApplication.nativeApplication.icon.bitmaps = [bitmap.bitmapData];
			if (air.NativeApplication.supportsSystemTrayIcon)
			{
				air.NativeApplication.nativeApplication.icon.tooltip = getAppName();
			}
	    	
			// set menu
			var menu = new air.NativeMenu();
			
			//Active Windows
			var item_show = new air.NativeMenuItem('Active');
			item_show.addEventListener(air.Event.SELECT , showApplication);
			menu.addItem(item_show);
			
			//Hidden Windows
			var item_hide = new air.NativeMenuItem('Hidden');
			item_hide.addEventListener( air.Event.SELECT , hide);
			menu.addItem(item_hide);
			
			//--
			var item_sel = new air.NativeMenuItem("", true);
			menu.addItem(item_sel);
			
			//Reset Windows
			var item_reset = new air.NativeMenuItem('Reset');
			item_reset.addEventListener( air.Event.SELECT , reset_size);
			menu.addItem(item_reset);
						
			//--
			var item_sel = new air.NativeMenuItem("", true);
			menu.addItem(item_sel);
			//Exit
			var item_exit = new air.NativeMenuItem('Exit');
			item_exit.addEventListener(air.Event.SELECT , onExit);
			menu.addItem(item_exit);
		
			//Link to menu...
			//air.NativeApplication.nativeApplication.autoExit  = false;
			air.NativeApplication.nativeApplication.icon.menu = menu;
			air.NativeApplication.nativeApplication.icon.addEventListener(window.runtime.flash.events.MouseEvent.CLICK, showApplication);
		}
	}
	
	context.instance 	= new _iconTray();
	return context.instance;
}